This article mainly introduces the code for implementing the addClass, removeClass, and hasClass functions using native javascript. if you need code, refer to the native addClass, removeClass, and hasClass functions without using jquery.
Class. js
Function hasClass (ele, cls) {return ele. className. match (new
This article mainly introduces the code for implementing the addClass, removeClass, and hasClass functions using native javascript. If you need code, refer to the native addClass, removeClass, and hasClass functions without using jquery.
Class. js
Function hasClass (ele, cls) {return ele. className. match (new
!= "'){ varArrclassname=Obj.className.split (' ');//split the original class to see if it contains the class that needs to be removed, that is, the classname of the reference CLS; varIndex=Arrindexof (ARRCLASSNAME,CLS); //R If there is a class that needs to be removed if(Index!= -1) {arrclassname.splice (index,1); Obj.classname=Arrclassname.join ("'); } } } functionHasclass (obj,cls) {varCLS=CLS|| "'; if(Cls.replace (/\s/G,"'). Length== 0){
Native AddClass, Removeclass, hasclass function, not using jquery
Class.js
function Hasclass (ele,cls) {return
Ele.className.match (New RegExp (' (\\s|^) ' +cls+ ' (\\s|$) '));
}
function AddClass (ele,cls) {
if (!this.hasclass (ele,cls)) Ele.classname = = "" +cls
;
function Removeclass (ele,cls) {
if (hasclass (ele,cls)) {
var reg = new RegEx
This article mainly introduces how to use JS to implement jQuery's addClass, removeClass, and hasClass functions. If you need a friend, you can refer to the following nonsense and directly go to the code.
The Code is as follows:
Function addClass (obj, cls ){Var obj_class = obj. className, // obtain the class content.Blank = (obj_class! = '')? '':''; // Determines whether the obtained class is empty. If not, add a 'space' to the front '.Added = obj
obj_class = obj. className, // obtain the class content. blank = (obj_class! = '')? '':''; // Determines whether the obtained class is empty. If not, add a 'space' to the front '. added = obj_class + blank + cls; // combines the original class with the class to be added. obj. className = added; // replace the original class .} function removeClass (obj, cls) {var obj_class = ''+ obj. className + ''; // obtain the class content and add a space at the beginning and end. ex) 'abc bcd'-> 'abc bcd'
Righteousness and usage
The Hasclass () method checks whether the selected element contains the specified class.
Grammar
$ (selector). Hasclass (Class)
Parameter description
Class required. Specifies the class that needs to be found in the specified element.
. Hasclass ("ClassName"): For the. Hasclass () method,
class is empty, and add a ' space ' to the front if it is not empty.
Added = Obj_class + blank + cls;//combines the original class and the class that needs to be added.
Obj.classname = added;//replaces the original class. The function removeclass (obj, cls) {var obj_class = ' +obj.classname+ ';//Gets the class content and adds a space to the end. ex) ' ABC BCD '- Gt ' abc BCD ' OBJ_CLASS = Obj_class.replace (/(\s+)/gi, '),//replaces extra empty characters with a single space. EX) ' ABC BCD '-
Determines whether there is at least one element in the matching collection using the style 'selected', the presence returns ' true ' and does not exist as ' flase '.HTML> Head> Scriptsrc= "Http://code.jquery.com/jquery-latest.js">Script> Script>$ (document). Ready (function(){ $("DIV#RESULT1"). Append ($ ("P:first"). Hasclass ("selected"). toString ()); $("DIV#RESULT2"). Append ($ ("P:last"). Hasclass
Interpretation of jQuery source code with hasClass () method analysis, jqueryhasclass
This article analyzes in detail the hasClass () method of jQuery source code interpretation. Share it with you for your reference. The specific analysis is as follows:
Copy codeThe Code is as follows: jQuery. fn. extend ({HasClass: function (selector ){// The selector of the cla
This article mainly introduces the hasClass () method of jQuery source code interpretation, and analyzes the implementation skills of the hasClass () method in a more detailed manner in the form of annotations, which has some reference value, for more information about jQuery source code, see the hasClass () method. Share it with you for your reference. The speci
Reference page:Http://www.w3school.com.cn/jquery/jquery_ref_attributes.asp1:addclass () Romoveclass (): Adds the specified class name to the matching element one or more, lethal horizon one or more class attributes: (if you add more than one class, you can separate the class name with a space)A: To add a class to the first P element:$ ("P:first"). AddClass ("Intro");B: Adding a class using a function C : to in the selected element Add two classes: Multiple classes are separated by spaces to :
Nonsense not much to say that directly on the code
Copy Code code as follows:
function addclass (obj, CLS) {
var Obj_class = obj.classname,//Gets the class content.
Blank = (Obj_class!= ")? ":"//To determine whether the acquired class is empty, and add a ' space ' to the front if it is not empty.
Added = Obj_class + blank + cls;//combines the original class and the class that needs to be added.
Obj.classname = added;//replaces the original class.
}
function Removeclass (obj,
Use JS to implement jQuery's addClass, removeClass, hasClass function, jqueryaddclass
Let's just talk about the code.
Copy codeThe Code is as follows:Function addClass (obj, cls ){Var obj_class = obj. className, // obtain the class content.Blank = (obj_class! = '')? '':''; // Determines whether the obtained class is empty. If not, add a 'space' to the front '.Added = obj_class + blank + cls; // combines the original class with the class to be added.Ob
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.